﻿using System;
using System.Collections.Generic;
using System.Windows;
using theeg.GraphWpf.Models;

namespace theeg.GraphWpf.Arrangement
{
    internal class Circular
    {
        internal void Arrange(double gridSize1, double gridSize2, List<Node> nodesToArrange)
        {
            if (nodesToArrange.Count <= 2)
                return;

            Node maximumDegree = nodesToArrange[0];

            int degree = nodesToArrange[0].IncomingEdges.Count + nodesToArrange[0].OutgoingEdges.Count;
            for (int i = 1; i < nodesToArrange.Count; i++)
            {
                if (nodesToArrange[i].IncomingEdges.Count + nodesToArrange[i].OutgoingEdges.Count > degree)
                {
                    degree = nodesToArrange[i].IncomingEdges.Count + nodesToArrange[i].OutgoingEdges.Count;
                    maximumDegree = nodesToArrange[i];
                }
            }

            int radius = (int)((float)gridSize1 / (1.5 * Math.Tan(Math.PI / (float)(nodesToArrange.Count - 1))));
            maximumDegree.Left = 100 + radius;
            maximumDegree.Top = 100 + radius;
            bool overMax = false;
            for (int i = 0; i < nodesToArrange.Count; i++)
            {
                if (nodesToArrange[i] == maximumDegree)
                {
                    overMax = true;
                }
                else
                {
                    Point target = new Point(100 + radius, 100 + radius);
                    float alpha = (float)((2.0 * Math.PI * (overMax ? i - 1 : i)) / (nodesToArrange.Count - 1));
                    target.X += (int)(Math.Sin(alpha) * radius);
                    target.Y -= (int)(Math.Cos(alpha) * radius);
                    nodesToArrange[i].Left = target.X;
                    nodesToArrange[i].Top = target.Y;
                }
            }
        }
    }
}
